//
//  MyTabBarController.m
//  JianTingSwift
//
//  Created by zhibao on 2024/6/18.
//

#import "MyTabBarController.h"

@interface MyTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MyTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    if([MYGlobal share].wechatFlag == 1){
        self.tabBar.backgroundColor = UIColor.whiteColor;
    }else{
        self.tabBar.backgroundImage = [[UIImage alloc]init];
    }
    self.tabBar.translucent = NO;
    self.delegate = self;
    self.tabBar.layer.shadowColor = [UIColor colorWithRed:17/255.0f green:18/255.0f blue:18/255.0f alpha:1].CGColor;
    self.tabBar.layer.shadowOffset = CGSizeMake(0, 1);
    self.tabBar.layer.shadowOpacity = 0.4;
    self.tabBar.layer.shadowRadius = 3;
    
    if (@available(iOS 15.0, *)) {
        
        UITabBarAppearance *bar = [UITabBarAppearance new];
        
        bar.backgroundEffect = nil;
        
        bar.stackedLayoutAppearance.selected.titleTextAttributes = @{NSFontAttributeName:MY_SCALE_BoldFont(10),NSForegroundColorAttributeName:UIColor.redColor};
        
        bar.stackedLayoutAppearance.normal.titleTextAttributes = @{NSFontAttributeName:MY_SCALE_BoldFont(10),NSForegroundColorAttributeName:MyColor(@"888888")};
        bar.stackedLayoutAppearance.normal.iconColor = MyColor(@"888888");
        bar.stackedLayoutAppearance.selected.iconColor = UIColor.redColor;
        
        self.tabBar.scrollEdgeAppearance = bar;
        self.tabBar.standardAppearance = bar;
        
    } else {
        
        self.tabBar.backgroundColor = [UIColor whiteColor];
        
        self.tabBar.barTintColor = [UIColor whiteColor];
        
        self.tabBar.translucent = NO;
        
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor} forState:UIControlStateNormal];
        
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
        
    }
    
    [self addChildVCs];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    //    LNavigationViewController *vc = (LNavigationViewController *)viewController;
    return YES;
}
// Do any additional setup after loading the view.
- (void)dealloc {
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}

- (void)addChildVCs {
    
    if([MYGlobal share].wechatFlag == 1){
        [self addChildVC:[[TempHomeController alloc] init] title: @"首页" imageName:@"bar_1"];
        [self addChildVC:[[DingListController alloc] init] title:@"订单列表" imageName:@"bar_2"];
    }else{
        [self addChildVC:[[HomeController alloc] init] title: @"首页" imageName:@"bar_1"];
        [self addChildVC:[[JianTingListController alloc] init] title:@"历史录音" imageName:@"bar_2"];
    }
    [self addChildVC:[[MeController alloc]init] title:@"我的" imageName:@"bar_3"];
}

- (void)addChildVC:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName {
    NSString *name2 = [NSString stringWithFormat:@"%@_selected",imageName];
    vc.tabBarItem.title = title;
    UIImage *lastImg = [Helper imageWithColor:MyColor(@"888888") withImage:[UIImage imageNamed:imageName]];
    vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[lastImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:0];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:name2] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    if (@available(iOS 13.0, *)) {
        [self.tabBar.standardAppearance qmui_applyItemAppearanceWithBlock:^(UITabBarItemAppearance * _Nonnull itemAppearance) {
            itemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, 0);
        }];
    } else {
        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 0);
    }
    LNavigationViewController *nav = [LNavigationViewController rootVC:vc];
    nav.gk_openScrollLeftPush = YES;
    nav.gk_openSystemNavHandle = YES;
    [self addChildViewController:nav];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    //    if (self.tabBar.items.count > 2) {
    //        UITabBarItem *item = self.tabBar.items[2];
    //        item.imageInsets = UIEdgeInsetsMake(-25, 0, 0, 0);
    //    }
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    //    NSLog(@"监听的是item文字: %@", item.title);
    //    NSInteger index = [self.tabBar.items indexOfObject:item];
    //    if (index != self.indexFlag) {
    //        NSInteger index = [self.tabBar.items indexOfObject:item];
    //           if (index != self.indexFlag) {
    //               //执行动画
    //               NSMutableArray *arry = [NSMutableArray array];
    //               for (UIView *btn in self.tabBar.subviews) {
    //                   if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
    //                        [arry addObject:btn];
    //                   }
    //               }
    //               //添加动画
    //               //放大效果，并回到原位
    //               CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //               //速度控制函数，控制动画运行的节奏
    //               animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //               animation.duration = 0.2;       //执行时间
    //               animation.repeatCount = 1;      //执行次数
    //               animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
    //               animation.fromValue = [NSNumber numberWithFloat:0.9];   //初始伸缩倍数
    //               animation.toValue = [NSNumber numberWithFloat:1.1];     //结束伸缩倍数
    //               UIView *curren = (UIView *)arry[index];
    //               [[curren layer] addAnimation:animation forKey:nil];
    //
    //               self.indexFlag = index;
    //           }
    //    }
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
